<?php
/*------------------------------------------------------ */
//-- 微信API核心调用
//-- @author iqgmy
/*------------------------------------------------------ */

namespace app\weixin\model;

use app\BaseModel;
use think\facade\Cache;

class WeiXinAccessTokenModel extends BaseModel
{
    protected $table = 'weixin_users_access_token';
    public $pk = 'id';
    public $mkey = 'users_access_token_';
    /**
     * [getUserAccessToken 获取AccessToken]
     * @param  string  $openid    [openid]
     * @param  boolean $curlagain [是否开启缓存]
     */
    public function getUserAccessToken($openid = '',$curlagain = false)
    {

        $AccessToken = Cache::get($this->mkey.$openid);
        $AccessToken = json_decode($AccessToken,'true');
        if (empty($AccessToken) && $curlagain == false) {
            $AccessToken = $this->where([['openid','=',$openid]])->find();
            if (empty($AccessToken) == false) {
                $AccessToken = $AccessToken->toArray();
            }
        }

        if (empty($AccessToken) || $curlagain || $AccessToken['expires_in'] < time()) {
            $SetConfig = settings();
            $ch = curl_init();
            // https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN
            curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/sns/oauth2/refresh_token?grant_type=refresh_token&appid=" . $SetConfig['weixin_appid'] . "&refresh_token=" . $AccessToken['refresh_token']);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
            //curl_setopt($ch, CURLOPT_POSTFIELDS, $par['data']);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $tmpInfo = curl_exec($ch);
            if (curl_errno($ch)) {
                return curl_error($ch);
            }
            curl_close($ch);
            $AccessToken = json_decode($tmpInfo, true);
            $this->saveUserAccessToken($AccessToken);
            $token = $AccessToken['access_token'];
        }

        return $AccessToken['access_token'];
    }
    /**
     * [saveUserAccessToken 保存token]
     * @param  array  $data [token信息]
     */
    public function saveUserAccessToken($data=[])
    {
        $data['expires_in'] = $data['expires_in'] + time();
        Cache::set($this->mkey.$data['openid'],json_encode($data),7200);
        $WeiXinAccessToken = $this->where('openid',$data['openid'])->find();
        if (empty($WeiXinAccessToken)) {
            $res = $this->save($data);
        }else{
            $WeiXinAccessToken->access_token = $data['access_token'];
            $WeiXinAccessToken->expires_in = $data['expires_in'];
            $WeiXinAccessToken->refresh_token = $data['refresh_token'];
            $WeiXinAccessToken->scope = $data['scope']; 
            $res = $WeiXinAccessToken->save();
        }
        return true;
    }
}

?>